﻿using System;
using CourseManager.Model;
using Ext.Net;

namespace CourseManager.View
{
	public partial class MainContent : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			
			if(!IsPostBack)
			{
				MakeNavigationTreePanel();
			}
		}

		private void MakeNavigationTreePanel()
		{
			int accountType;
			if (Session["AccountType"] != null && int.TryParse(Session["AccountType"].ToString(), out accountType))
			{
				switch(accountType)
				{
					case (int)RoleType.STUDENT:
						LoadStudent();
						break;
					case (int)RoleType.ADMINISTARTOR:
						LoadAdmin();
						break;
					case (int)RoleType.TEACHER:
						LoadTeacher();
						break;
					default:
						Response.Redirect("ErrorPage.htm");
						break;
				}
			}
			else
			{
				Response.Redirect("ErrorPage.htm");
			}
			
		}

		private void LoadTeacher()
		{
			var root = (TreeNode)navPnl.Root[0];
			root.Nodes.Clear();
			var node1 = new TreeNode("课程管理");
			node1.Nodes.Add(CreateNode("任教课程"));
			node1.Nodes.Add(CreateNode("查看学生"));
			root.Nodes.Add(node1);
			navPnl.ExpandAll();
		}

		private void LoadStudent()
		{
			var root = (TreeNode)navPnl.Root[0];
			root.Nodes.Clear();
			var node1 = new TreeNode("课程管理");
			node1.Nodes.Add(CreateNode("选课"));
			node1.Nodes.Add(CreateNode("课表查询"));
			var node2 = new TreeNode("综合查询");
			node2.Nodes.Add(CreateNode("考试日程"));
			node2.Nodes.Add(CreateNode("CET成绩"));
			node2.Nodes.Add(CreateNode("学期成绩"));
			var node3 = new TreeNode("个人信息");
			node3.Nodes.Add(CreateNode("修改密码"));
			root.Nodes.Add(node1);
			root.Nodes.Add(node2);
			root.Nodes.Add(node3);
			navPnl.ExpandAll();
		}

		private void LoadAdmin()
		{
			var root = (TreeNode)navPnl.Root[0];
			root.Nodes.Clear();
			var node1 = new TreeNode("用户管理");
			node1.Nodes.Add(CreateNode("教师管理"));
			node1.Nodes.Add(CreateNode("学生管理"));

			var node2 = new TreeNode("课程管理");
			node2.Nodes.Add(CreateNode("课程管理"));
			var node3 = new TreeNode("个人平台");
			node3.Nodes.Add(CreateNode("密码设置"));
			root.Nodes.Add(node1);
			root.Nodes.Add(node2);
			root.Nodes.Add(CreateNode("系统设置"));
			root.Nodes.Add(node3);
			navPnl.ExpandAll();
		}

		private static TreeNode CreateNode(string name)
		{
			var node = new TreeNode(name);
			node.Listeners.Click.Handler = "ShowContent('" + name + "');";
			return node;
		}

		[DirectMethod]
		public void ShowContent(string nid)
		{
			switch(nid)
			{
				case "选课":
					LoadContent(Constants.STUDENT_SELECT_COURSE);
					break;
				case "角色管理":
					LoadContent(Constants.ADMIN_ROLE_MANAGEMENT);
					break;
				case "学生管理":
					LoadContent(Constants.ADMIN_STUDENT_MANAGEMENT);
					break;
				case "教师管理":
					LoadContent(Constants.ADMIN_TEACHER_MANAGEMENT);
					break;
				case "系统设置":
					LoadContent(Constants.ADMIN_SYSTEM_MANAGEMENT);
					break;
				case "课程管理":
					LoadContent(Constants.ADMIN_COURSE_MANAGEMENT);
					break;
				case "密码设置":
					LoadContent(Constants.CHANGE_PASSWORD);
					break;
				case "课表查询":
					LoadContent(Constants.STUDENT_COURSE_SCHEDULE);
					break;
				case "任教课程":
					LoadContent(Constants.TEACHER_COURSE_SCHEDULE);
					break;
				case "查看学生":
					LoadContent(Constants.TEACHER_COURSE_INFO);
					break;
			}
		}

		private void LoadContent(String path)
		{
			Content.LoadContent(new LoadConfig(path, LoadMode.IFrame, true));
		}
	}

	
}